package com.palfund.animationandanimator;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class PropertyActivity extends AppCompatActivity {
    private ImageView mImageView;
    private ObjectAnimator mAnimator4X;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween);
        mImageView = (ImageView) findViewById(R.id.imageView);
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public void click(View view) {
        switch (view.getId()) {
            case R.id.btn_alpha_xml:
                ObjectAnimator alpha_xml = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R
                        .animator.alpha);
                alpha_xml.setTarget(mImageView);
                alpha_xml.start();
                break;
            case R.id.btn_rotate_xml:
                ObjectAnimator rotate_xml = (ObjectAnimator) AnimatorInflater.loadAnimator(this,
                        R.animator.rotate);
                rotate_xml.setTarget(mImageView);
                rotate_xml.start();
                break;
            case R.id.btn_scale_xml:
                AnimatorSet scale_xml = (AnimatorSet) AnimatorInflater.loadAnimator(this, R
                        .animator.scale);
                scale_xml.setTarget(mImageView);
                scale_xml.start();
                break;
            case R.id.btn_translation_xml:
                AnimatorSet translate_xml = (AnimatorSet) AnimatorInflater.loadAnimator(this, R
                        .animator.translate);
                translate_xml.setTarget(mImageView);
                translate_xml.start();
                break;
            case R.id.btn_set_xml:
                AnimatorSet animatorSet_xml = (AnimatorSet) AnimatorInflater.loadAnimator(this, R
                        .animator.set_animator);
                animatorSet_xml.setTarget(mImageView);
                animatorSet_xml.start();
                break;
            case R.id.btn_alpha:
                ObjectAnimator animator_alpha = ObjectAnimator.ofFloat(mImageView, "alpha", 1.0f,
                        0.0f);
                animator_alpha.setDuration(3000);
                animator_alpha.setRepeatCount(ValueAnimator.INFINITE);
                animator_alpha.setRepeatMode(ValueAnimator.REVERSE);
                animator_alpha.start();
                break;
            case R.id.btn_rotate:
                ObjectAnimator animator_rotate = ObjectAnimator.ofFloat(mImageView, "rotation",
                        0, 360);
                animator_rotate.setDuration(3000);
                animator_rotate.setRepeatCount(ValueAnimator.INFINITE);
                animator_rotate.setRepeatMode(ValueAnimator.REVERSE);
                animator_rotate.start();
                break;
            case R.id.btn_scale:
                ObjectAnimator animator_scaleX = ObjectAnimator.ofFloat(mImageView, "scaleX", 0,
                        2.0f);
                ObjectAnimator animator_scaleY = ObjectAnimator.ofFloat(mImageView, "scaleY", 0,
                        2.0f);
                animator_scaleX.setDuration(3000);
                animator_scaleX.setRepeatCount(ValueAnimator.INFINITE);
                animator_scaleX.setRepeatMode(ValueAnimator.REVERSE);
                animator_scaleX.start();
                animator_scaleY.setDuration(3000);
                animator_scaleY.setRepeatCount(ValueAnimator.INFINITE);
                animator_scaleY.setRepeatMode(ValueAnimator.REVERSE);
                animator_scaleY.start();
                break;
            case R.id.btn_translation:
                mAnimator4X = ObjectAnimator.ofFloat(mImageView, "translationX", 0, 200);
                ObjectAnimator animator4Y = ObjectAnimator.ofFloat(mImageView, "translationY", 0,
                        100);
                //添加执行过程监听器
                mAnimator4X.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        Log.i("PropertyActivity", "---onAnimationStart()--->" + Thread
                                .currentThread().getName());
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        Log.i("PropertyActivity", "---onAnimationEnd()--->" + Thread
                                .currentThread().getName());
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        Log.i("PropertyActivity", "---onAnimationCancel()--->" + Thread
                                .currentThread().getName());
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {
                        Log.i("PropertyActivity", "---onAnimationRepeat()--->" + Thread
                                .currentThread().getName());
                    }
                });
                //添加暂停监听
                mAnimator4X.addPauseListener(new Animator.AnimatorPauseListener() {
                    @Override
                    public void onAnimationPause(Animator animation) {

                    }

                    @Override
                    public void onAnimationResume(Animator animation) {

                    }
                });
                //动画数值变化监听
                mAnimator4X.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                    }
                });
                mAnimator4X.setDuration(3000);
                mAnimator4X.setRepeatCount(ValueAnimator.INFINITE);
                mAnimator4X.setRepeatMode(ValueAnimator.REVERSE);
                mAnimator4X.start();
                animator4Y.setDuration(3000);
                animator4Y.setRepeatCount(ValueAnimator.INFINITE);
                animator4Y.setRepeatMode(ValueAnimator.REVERSE);
                animator4Y.start();
                break;
            case R.id.btn_set:
                AnimatorSet animatorSet = new AnimatorSet();
                //从左到右侧
                ObjectAnimator animator_1X = ObjectAnimator.ofFloat(mImageView, "translationX",
                        0, 200);
                animator_1X.setDuration(3000);
                animator_1X.setInterpolator(new BounceInterpolator());
                //animator_1X.start();
                ObjectAnimator animator_1Y = ObjectAnimator.ofFloat(mImageView, "translationY",
                        0, 200);
                animator_1Y.setDuration(3000);
                animator_1Y.setInterpolator(new BounceInterpolator());
                //animator_1Y.start();
                ObjectAnimator animator_2X = ObjectAnimator.ofFloat(mImageView, "translationX",
                        200, 0);
                animator_2X.setDuration(3000);
                animator_2X.setInterpolator(new BounceInterpolator());
                //animator_2X.start();
                ObjectAnimator animator_2Y = ObjectAnimator.ofFloat(mImageView, "translationY",
                        200, 0);
                animator_2Y.setDuration(3000);
                animator_2Y.setInterpolator(new DecelerateInterpolator());
                //animator_2Y.start();
                List<Animator> list = new ArrayList<>();
                list.add(animator_1X);
                list.add(animator_1Y);
                list.add(animator_2X);
                list.add(animator_2Y);
                //让定义的 每个动画依次执行
                //animatorSet.playTogether(list);
                animatorSet.playSequentially(list);
                animatorSet.start();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //mAnimator4X.cancel();
    }
}
